{% if use_page is undefined %}
  {% set use_page = currentpage %}
{% endif %}
{% if link_prefix is undefined %}
  {% set link_prefix = "" %}
{% endif %}
<div class="tree_nav">

  <!--{############# Parent page link ###############################}-->
  {% if use_page.supercategory is defined %}
    <div class="sidenav_parent">
      {% if use_page == (pages|selectattr('supercategory', 'defined_and_equalto', use_page.supercategory)|first) %}
      <a  href="{{ link_prefix}}{{ use_page.html }}">{{ use_page.name }}</a>
      {% else %}
      {% set parent_page = (pages|selectattr('supercategory', 'defined_and_equalto', use_page.supercategory)|first) %}
      <a href="{{ link_prefix}}{{ parent_page.html }}"> {{ parent_page.name }}</a>
      {% endif %}
    </div>
  {% elif use_page.doc_type is defined %}
    <div class="sidenav_parent">
      {% if use_page == (pages|selectattr('doc_type', 'defined_and_equalto', use_page.doc_type)|first) %}
      <a  href="{{ link_prefix}}{{ use_page.html }}">{{ use_page.name }}</a>
      {% else %}
      {% set parent_page = (pages|selectattr('doc_type', 'defined_and_equalto', use_page.doc_type)|first) %}
      <a href="{{ link_prefix}}{{ parent_page.html }}"> {{ parent_page.name }}</a>
      {% endif %}
    </div>
  {% elif use_page.funnel is defined %}
    <div class="sidenav_parent">
      {% if use_page == (pages|selectattr('funnel', 'defined_and_equalto', use_page.funnel)|first) %}
      <a  href="{{ link_prefix}}{{ use_page.html }}">{{ use_page.name }}</a>
      {% else %}
      {% set parent_page = (pages|selectattr('funnel', 'defined_and_equalto', use_page.funnel)|first) %}
      <a href="{{ link_prefix}}{{ parent_page.html }}"> {{ parent_page.name }}</a>
      {% endif %}
    </div>
  {% endif %}

  <!--{############# Sidebar links ###############################}-->

  {% if not pages|selectattr("funnel", 'defined_and_equalto', use_page.funnel)|selectattr("category", "defined")|list|length %}
    <!--{##### Case: no funnel categories, simple one-category sidebar ####}-->
    {% set funnelpages = pages|selectattr("funnel", 'defined_and_equalto', use_page.funnel)|list %}
    <div id="sidenav" aria-multiselectable="true">
      <div class="card active">
        <div class="card-body card-body-single">
          <ul class="sidebar_pagelist">
          {% for page in funnelpages %}
            {% if loop.index == 1 %}{# Skip the first element since it's linked by the funnel header #}
            {% elif page.template == "template-redirect.html" %}{# skip redirects #}
            {% elif page == use_page %}
            <li><a class="active nosubcat-page" href="{{ link_prefix}}{{ page.html }}">{{ page.name }}{% if page.status is defined and page.status == "not_enabled" %} {% include 'template-status_not_enabled.html' %}{% endif %}</a></li>
            {% else %}
            <li><a class="nosubcat-page" href="{{ link_prefix}}{{ page.html }}">{{ page.name }}{% if page.status is defined and page.status == "not_enabled" %} {% include 'template-status_not_enabled.html' %}{% endif %}</a></li>
            {% endif %}
          {% endfor %}
          {% if use_page.funnel == "Dev Tools" %}
          <li><a class="nosubcat-page" href="https://livenet.xrpl.org/" target="_blank">XRP Ledger Explorer <i class="fa fa-external-link"></i></a></li>
          {% endif %}
          </ul>
        </div><!-- /.card-body -->
      </div><!-- /.card -->
    </div><!-- /#sidenav -->

  {% elif use_page.supercategory is defined %}
    <!--{# Case: in a supercategory, show cards for that supercategory's categories #}-->
    <div id="sidenav" role="tablist" aria-multiselectable="true">
      {% for cat in categories %}
        {% set catpages = pages|selectattr("category", 'defined_and_equalto', cat)|list %}
        {% if use_page.funnel is defined %}
          {% set catpages = catpages|selectattr("funnel", 'defined_and_equalto', use_page.funnel)|list %}
        {% endif %}
        {% if use_page.doc_type is defined %}
          {% set catpages = catpages|selectattr("doc_type", 'defined_and_equalto', use_page.doc_type)|list %}
        {% endif %}
        {% set catpages = catpages|selectattr("supercategory", 'defined_and_equalto', use_page.supercategory)|list %}
        {% if catpages|length %}
          {% set cat_parent = pages|selectattr('category', 'defined_and_equalto', cat)|first %}
            <div class="card{% if use_page.category is defined and use_page.category == cat %} active{% endif %}">
              <div class="card-header" role="tab" id="sidenav_cat_head_{{loop.index}}">
                {% if catpages|length > 1 %}
                <a class="{% if use_page.category is undefined or use_page.category != cat %}collapsed {% endif %}sidenav_cat_toggler" role="button" data-toggle="collapse" href="#sidenav_collapse_{{loop.index}}" aria-expanded="true" aria-controls="sidenav_collapse_{{loop.index}}"></a>
                {% endif %}
                <h5 class="card-title">
                    <a class="sidenav_cat_title{% if use_page == (cat_parent) %} active{% elif use_page.category is defined and use_page.category == cat %} active-parent{% endif %}" href="{{ link_prefix}}{{ cat_parent.html }}">{{ cat_parent.name }}</a>
                </h5>
              </div><!-- /.card-header -->

              <div id="sidenav_collapse_{{loop.index}}" class="collapse{% if use_page.category is defined and use_page.category == cat %} show{% endif %}" role="tabpanel" aria-labelledby="sidenav_cat_head_{{loop.index}}">
                <div class="card-body">
                  <ul class="sidebar_pagelist">
                  {% set printed_subcategories = [] %}
                  {% for page in catpages %}
                    {% if loop.index != 1 %}{# Skip the first element since it's linked by the category header #}
                      {% if page.template == "template-redirect.html" %}{# skip redirects #}
                      {% elif page.subcategory is undefined %}
                        {% if page == use_page %}
                          <li><a class="active nosubcat-page" href="{{ link_prefix}}{{ page.html }}">{{ page.name }}{% if page.status is defined and page.status == "not_enabled" %} {% include 'template-status_not_enabled.html' %}{% endif %}</a></li>
                        {% else %}
                        <li><a class="nosubcat-page" href="{{ link_prefix}}{{ page.html }}">{{ page.name }}{% if page.status is defined and page.status == "not_enabled" %} {% include 'template-status_not_enabled.html' %}{% endif %}</a></li>
                        {% endif %}
                      {% elif page.subcategory not in printed_subcategories %}
                        {% if page == use_page %}
                        <li><a class="subcat-title active" href="#main_content_body">{{ page.name }}{% if page.status is defined and page.status == "not_enabled" %} {% include 'template-status_not_enabled.html' %}{% endif %}</a></li>
                        {% elif use_page.subcategory is defined and page.subcategory == use_page.subcategory %}
                        <li><a class="subcat-title active-parent" href="{{ link_prefix}}{{ page.html }}">{{ page.name }}{% if page.status is defined and page.status == "not_enabled" %} {% include 'template-status_not_enabled.html' %}{% endif %}</a></li>
                        {% else %}
                        <li><a class="subcat-title" href="{{ link_prefix}}{{ page.html }}">{{ page.name }}{% if page.status is defined and page.status == "not_enabled" %} {% include 'template-status_not_enabled.html' %}{% endif %}</a></li>
                        {% endif %}

                        {% for subpage in catpages|selectattr('subcategory', 'defined_and_equalto', page.subcategory) %}
                          {% if subpage != page and subpage.template != "template-redirect.html" %}
                            {% if subpage == use_page %}
                            <li><a class="active subpage" href="#main_content_body">{{ subpage.name }}{% if subpage.status is defined and subpage.status == "not_enabled" %} {% include 'template-status_not_enabled.html' %}{% endif %}</a></li>
                            {% else %}
                            <li><a class="subpage" href="{{ link_prefix}}{{ subpage.html }}">{{ subpage.name }}{% if subpage.status is defined and subpage.status == "not_enabled" %} {% include 'template-status_not_enabled.html' %}{% endif %}</a></li>
                            {% endif %}
                          {% endif %}
                        {% endfor %}
                        {% set _ = printed_subcategories.append(page.subcategory) %}

                      {% endif %}
                    {% endif %}
                  {% endfor %}

                  </ul>
                </div><!-- /.card-body -->
              </div><!-- /#sidenav_collapse{{loop.index}} -->

            </div><!-- /.panel -->
        {% endif %}
      {% endfor %}
    </div><!-- /#sidenav -->

  {% else %}
    <!--{##### Case: Not in a supercategory, but siblings might be #}-->
    {% set sidebar_pagelist = pages|selectattr("funnel", 'defined_and_equalto', use_page.funnel)|list %}
    {% if use_page.doc_type is defined %}
      {% set sidebar_pagelist = sidebar_pagelist|selectattr("doc_type", 'defined_and_equalto', use_page.doc_type)|list %}
    {% endif %}
    {% set printed_modules = [] %}
    <div id="sidenav" role="tablist" aria-multiselectable="true">
    {% for page in sidebar_pagelist %}
      {% if page.supercategory is defined %}
        {% if page.supercategory not in printed_modules %}
          <!--{################# Supercategory Card ####################}-->
          {% set supercat = page.supercategory %}
          {% set supercatpages = sidebar_pagelist|selectattr("supercategory", 'defined_and_equalto', page.supercategory)|list %}
          {% if supercatpages|length %}
            {% set supercat_parent = supercatpages|first %}
            {% set _ = printed_modules.append(page.supercategory) %}
            <div class="card">
              <div class="card-header" role="tab" id="sidenav_cat_head_{{loop.index}}">
                {% if supercatpages|length > 1 %}
                  <a class="collapsed sidenav_cat_toggler" role="button" data-toggle="collapse" href="#sidenav_collapse_{{loop.index}}" aria-expanded="true" aria-controls="sidenav_collapse_{{loop.index}}"></a>
                {% endif %}
                <h5 class="card-title">
                  <a class="sidenav_cat_title" href="{{ link_prefix}}{{ supercat_parent.html }}">{{ supercat_parent.name }}</a>
                </h5>
              </div><!-- /.card-header -->

              <div id="sidenav_collapse_{{loop.index}}" class="collapse" role="tabpanel" aria-labelledby="sidenav_cat_head_{{loop.index}}">
                <div class="card-body">
                  <ul class="sidebar_pagelist">
                  {% set printed_categories = [] %}
                  {% for subpage in supercatpages %}
                    {% if loop.index != 1 %}{# Skip the first element since it's linked by the supercategory header #}
                      {% if subpage.template == "template-redirect.html" %}{# skip redirects #}
                      {% elif subpage.category not in printed_categories %}
                        <li><a class="subcat-title" href="{{ link_prefix}}{{ subpage.html }}">{{ subpage.name }}{% if page.status is defined and page.status == "not_enabled" %} {% include 'template-status_not_enabled.html' %}{% endif %}</a></li>

                        {% set category_members = supercatpages|selectattr('category', 'defined_and_equalto', subpage.category)|list %}
                        {% for subsubpage in category_members %}
                          {% if subsubpage != subpage and (subsubpage.subcategory is undefined or
                              subsubpage == category_members|selectattr('subcategory', 'defined_and_equalto', subsubpage.subcategory)|first) and subsubpage.template != "template-redirect.html" %}
                            <li><a class="subpage" href="{{ link_prefix}}{{ subsubpage.html }}">{{ subsubpage.name }}{% if subsubpage.status is defined and subsubpage.status == "not_enabled" %} {% include 'template-status_not_enabled.html' %}{% endif %}</a></li>
                          {% endif %}
                        {% endfor %}
                        {% set _ = printed_categories.append(subpage.category) %}
                      {% endif %}

                    {% endif %}
                  {% endfor %}
                  </ul>
                </div><!-- /.card-body -->
              </div><!-- /#sidenav_collapse{{loop.index}} -->

            </div><!-- /.card -->

          {% endif %}
        {% endif %}




      {% elif page.category is defined and page.category not in printed_modules %}
        <!--{################# Category Card ####################}-->
        {% set cat = page.category %}
        {% set catpages = sidebar_pagelist|selectattr("category", 'defined_and_equalto', page.category)|list %}
        {% if catpages|length %}
          {% set cat_parent = catpages|first %}
          <div class="card{% if use_page.category is defined and use_page.category == cat %} active{% endif %}">
            <div class="card-header" role="tab" id="sidenav_cat_head_{{loop.index}}">
              {% if catpages|length > 1 %}
                <a class="{% if use_page.category is undefined or use_page.category != cat %}collapsed {% endif %}sidenav_cat_toggler" role="button" data-toggle="collapse" href="#sidenav_collapse_{{loop.index}}" aria-expanded="true" aria-controls="sidenav_collapse_{{loop.index}}"></a>
              {% endif %}
              <h5 class="card-title">
                  <a class="sidenav_cat_title{% if use_page == (cat_parent) %} active{% elif use_page.category is defined and use_page.category == cat %} active-parent{% endif %}" href="{{ link_prefix}}{{ cat_parent.html }}">{{ cat_parent.name }}</a>
                <!-- <a class="sidenav_cat_title" href="{{ link_prefix}}{{ cat_parent.html }}">{{ cat }}</a> -->
              </h5>
            </div><!-- /.card-header -->

            <div id="sidenav_collapse_{{loop.index}}" class="collapse{% if use_page.category is defined and use_page.category == cat %} show{% endif %}" role="tabpanel" aria-labelledby="sidenav_cat_head_{{loop.index}}">
              <div class="card-body">
                <ul class="sidebar_pagelist">
                {% set printed_subcategories = [] %}
                {% for page in catpages %}
                  {% if loop.index != 1 %}{# Skip the first element since it's linked by the category header #}
                    {% if page.template == "template-redirect.html" %}{# skip redirects #}
                    {% elif page.subcategory is undefined %}
                      {% if page == use_page %}
                      <li><a class="active nosubcat-page" href="{{ link_prefix}}{{ page.html }}">{{ page.name }}{% if page.status is defined and page.status == "not_enabled" %} {% include 'template-status_not_enabled.html' %}{% endif %}</a></li>
                      {% else %}
                      <li><a class="nosubcat-page" href="{{ link_prefix}}{{ page.html }}">{{ page.name }}{% if page.status is defined and page.status == "not_enabled" %} {% include 'template-status_not_enabled.html' %}{% endif %}</a></li>
                      {% endif %}
                    {% elif page.subcategory not in printed_subcategories %}
                      {% if page == use_page %}
                      <li><a class="subcat-title active" href="#main_content_body">{{ page.name }}{% if page.status is defined and page.status == "not_enabled" %} {% include 'template-status_not_enabled.html' %}{% endif %}</a></li>
                      {% elif page.subcategory is defined and use_page.subcategory is defined and page.subcategory == use_page.subcategory %}
                      <li><a class="subcat-title active-parent" href="{{ link_prefix}}{{ page.html }}">{{ page.name }}{% if page.status is defined and page.status == "not_enabled" %} {% include 'template-status_not_enabled.html' %}{% endif %}</a></li>
                      {% else %}
                      <li><a class="subcat-title" href="{{ link_prefix}}{{ page.html }}">{{ page.name }}{% if page.status is defined and page.status == "not_enabled" %} {% include 'template-status_not_enabled.html' %}{% endif %}</a></li>
                      {% endif %}

                      {% for subpage in catpages|selectattr('subcategory', 'defined_and_equalto', page.subcategory) %}
                        {% if subpage != page %}
                          {% if subpage == use_page %}
                          <li><a class="active subpage" href="#main_content_body">{{ subpage.name }}{% if subpage.status is defined and subpage.status == "not_enabled" %} {% include 'template-status_not_enabled.html' %}{% endif %}</a></li>
                          {% elif subpage.template != "template-redirect.html" %}
                          <li><a class="subpage" href="{{ link_prefix}}{{ subpage.html }}">{{ subpage.name }}{% if subpage.status is defined and subpage.status == "not_enabled" %} {% include 'template-status_not_enabled.html' %}{% endif %}</a></li>
                          {% endif %}
                        {% endif %}
                      {% endfor %}
                      {% set _ = printed_subcategories.append(page.subcategory) %}

                    {% endif %}
                  {% endif %}
                {% endfor %}

                </ul>
              </div><!-- /.card-body -->
            </div><!-- /#sidenav_collapse{{loop.index}} -->

          </div><!-- /.card -->
        {% endif %}
        {% set _ = printed_modules.append(page.category) %}

      {% endif %}
    {% endfor %}
    </div><!-- /#sidenav -->
  {% endif %}
</div><!-- /.tree_nav -->
